package com.instpower.things.handlers;

import com.instpower.things.config.Configs;
import com.instpower.things.instpower.Connection;
import com.instpower.things.instpower.Handler;
import com.instpower.things.transport.Request;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName Handler0F01Action
 * @Author: hellmash@gmail.com
 * @Date: 2020/6/8
 * @Version: 1.0.0
 * @Description: 升级握手
 */
//@Component(value = "CMD0F01")
public class Handler0F01Action extends Handler<Request> {

//    @Autowired
//    private InstService instService;

    @Override
    public short getId() {
        return Configs.UPDATE_START;
    }

    @Override
    public void execute(Connection connection, Request data) throws UnsupportedEncodingException {
        logger.info("Exec - CMD0F01 - BEGIN");
//        Map<String, Object> task = instService.findTaskByKey(4);
//        //单机实时更新
//        if("single_update".equals(task.get("ota_type"))) {
//            String sn = (String) task.get("cabinet_sn");
//            Response resp = new Response();
//            Header header = new Header();
//            header.setPtrol((byte) 0x02);
//            header.setAddr(sn);
//            header.setCommand((short) 0x0F05);
//
//            FirmwareInfo firmwareInfo = new FirmwareInfo();
//            firmwareInfo.setProtocol(FirmwareInfo.Protocol.HTTP);
//            firmwareInfo.setIpAddress("0.0.0.0");
//            firmwareInfo.setPort((short) 80);
//            firmwareInfo.setUsername("anker");
//            firmwareInfo.setPassword("anker");
//            firmwareInfo.setUrl((String)task.get("ota_url"));
//            firmwareInfo.setFileLenght((int)task.get("file_length"));
//            firmwareInfo.setCheckSum((int)task.get("check_sum"));
////                firmwareInfo.setUrl("https://instpower-test-bucket.s3-ap-southeast-1.amazonaws.com/firmware/AB_Box.207");
////                firmwareInfo.setFileLenght(59692);
////                firmwareInfo.setCheckSum(7591950);
//            MTData mtdata = new MTData();
//            DataBuffer dataBuffer = firmwareInfo.encode();
//            int headLength = dataBuffer.readableBytes();
//            mtdata.setDatas(dataBuffer.readBytes(headLength));
//
//            header.setLength((short)(headLength +2));
//            resp.setHeader(header);
//            resp.setData(mtdata);
//
//            DataBuffer buffer = resp.encode();
//            int checksum = Checksum.crc16(Checksum.CRC16_ANKER,
//                    ByteBuffer.wrap(buffer.readByteArray(buffer.readableBytes())));
//            Ender ender = new Ender();
//            ender.setChecksum(checksum);
//            resp.setEnder(ender);
//            connection.sendResponse(resp);
//        }
        logger.info("Exec - CMD0F01 - END");
    }
}
